package com.molyfun.parents.fragment.dialog;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.setting.WebViewActivity;
import com.molyfun.parents.fragment.BaseDialogFragment;
import com.molyfun.parents.rest.bean.MainActivityListResponse;
import com.molyfun.parents.view.circleViewpager.BannerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;


/**
 * 选择日期弹窗dialog
 */
public class MainActivityListDialogFragment extends BaseDialogFragment implements View.OnClickListener {

    private static final String FLAG_ACTIVITY_LIST = "activity_list_bean";
    BannerView mBannerView;
    ImageView mImageView;

    private ArrayList<MainActivityListResponse.MainActivityBean> activityBeanList;

    private static MainActivityListDialogFragment newInstance(ArrayList<MainActivityListResponse.MainActivityBean> activityBeanList) {
        MainActivityListDialogFragment fragment = new MainActivityListDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(FLAG_ACTIVITY_LIST, activityBeanList);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_activity_dialog, container, false);
        (view.findViewById(R.id.ic_close)).setOnClickListener(this);
        mBannerView = (BannerView) view.findViewById(R.id.banner1);
        mImageView = (ImageView) view.findViewById(R.id.main_activity_image);
        activityBeanList = getArguments().getParcelableArrayList(FLAG_ACTIVITY_LIST);

        if(activityBeanList.size() > 0){
            if(activityBeanList.size() == 1){
                mBannerView.setVisibility(View.GONE);
                mImageView.setVisibility(View.VISIBLE);
                if(!TextUtils.isEmpty(activityBeanList.get(0).getUrl())){
                    Picasso.with(getContext())
                            .load(activityBeanList.get(0).getUrl())
                            .placeholder(R.mipmap.ic_pic_holder)
                            .into(mImageView);
                }
                mImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        WebViewActivity.startActivity(getContext(), "http://activity.molyfun.com/pop/pop.html?url=" + activityBeanList.get(0).getActivityurl(), "活动详情");
                        getDialog().dismiss();
                    }
                });
            }else{
                mBannerView.setViewFactory(new BannerViewFactory());
                mBannerView.setDataList(activityBeanList);
                mBannerView.start();
            }
        }


        return view;
    }


    public class BannerViewFactory implements BannerView.ViewFactory<MainActivityListResponse.MainActivityBean> {
        @Override
        public View create(MainActivityListResponse.MainActivityBean item, int position, ViewGroup container) {
            ImageView iv = new ImageView(container.getContext());
            final MainActivityListResponse.MainActivityBean mainActivityBean = activityBeanList.get(position);
            if(!TextUtils.isEmpty(mainActivityBean.getUrl())){
                Picasso.with(getContext())
                        .load(mainActivityBean.getUrl())
                        .placeholder(R.mipmap.ic_pic_holder)
                        .into(iv);
            }
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    WebViewActivity.startActivity(getContext(), "http://activity.molyfun.com/pop/pop.html?url=" + mainActivityBean.getActivityurl(), "活动详情");
                    getDialog().dismiss();
                }
            });
            return iv;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.ic_close:
                break;
        }
        getDialog().dismiss();
    }

    public static class MainActivityListDialogBuilder {
        private MainActivityListDialogFragment mMainActivityListDialogFragment;
        private String mTag;

        public MainActivityListDialogBuilder(ArrayList<MainActivityListResponse.MainActivityBean> activityList) {
            mMainActivityListDialogFragment = newInstance(activityList);
            mTag = "main_activity_list";
        }

        public void build(FragmentTransaction fragmentTransaction) {
            fragmentTransaction.add(mMainActivityListDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }
    }

}
